package com.beanverify.inter.verifier.impl;

import java.lang.annotation.Annotation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.beanverify.annotations.Mob;
import com.beanverify.exceptions.UnKnownVerifyException;
import com.beanverify.exceptions.UnSupportedParamTypeException;
import com.beanverify.inter.AnnotationVerifier;
import com.beanverify.utils.UtilCommon;



public class VerifyMob implements AnnotationVerifier{
	private static final Logger LOG = LoggerFactory.getLogger(VerifyMob.class);

	public boolean doVerify(Object paramValue, String paramName, Annotation annotation)  throws UnSupportedParamTypeException, UnKnownVerifyException{
		try {
			String mob = (String)paramValue;
			Mob mobile = (Mob)annotation;
			if (true == mobile.cantNull() && UtilCommon.isNull(mob)){
				LOG.warn(String.format("[%s]参数不能为空--×",paramName));
				return false;
			}
			if (false == mobile.cantNull() && UtilCommon.isNull(mob)){
				LOG.warn(String.format("[%s]参数为空!无法受检Mob,但默认允许为空,可以通过--√",paramName));
				return true;
			}
			if (!UtilCommon.isMobNO(mob)){
				LOG.debug(String.format("[%s]Mob检查失败--×",paramName));
				return false;
			} else {
				LOG.debug(String.format("[%s]Mob检查通过--√",paramName));
				return true;
			}
		} catch(ClassCastException e){
			LOG.error("VerifyMob异常!",e);
			throw new UnSupportedParamTypeException("VerifyMob异常!");
		} catch(Exception e){
			LOG.error("未知的VerifyMob异常!",e);
			throw new UnKnownVerifyException("未知的VerifyMob异常!");
		}
	}
}